balance Quickstart (CBPS): Analyzing and adjusting the bias on a simulated toy dataset¶

'balance' is a Python package that is maintained and released by the Core Data Science Tel-Aviv team in Meta. 'balance' performs and evaluates bias reduction by weighting for a broad set of experimental and observational use cases.

Although balance is written in Python, you don't need a deep Python understanding to use it. In fact, you can just use this notebook, load your data, change some variables and re-run the notebook and produce your own weights!

This quickstart demonstrates re-weighting specific simulated data, but if you have a different usecase or want more comprehensive documentation, you can check out the comprehensive balance tutorial.

Analysis¶

There are four main steps to analysis with balance:

  • load data
  • check diagnostics before adjustment
  • perform adjustment + check diagnostics
  • output results

Let's dive right in!

Example dataset¶

The following is a toy simulated dataset.

In [1]:
from balance import load_data
INFO (2022-11-23 07:57:00,299) [__init__/<module> (line 52)]: Using balance version 0.1.1
In [2]:
target_df, sample_df = load_data()

print("target_df: \n", target_df.head())
print("sample_df: \n", sample_df.head())
target_df: 
        id gender age_group     income
0  100000   Male       45+  10.183951
1  100001   Male       45+   6.036858
2  100002   Male     35-44   5.226629
3  100003    NaN       45+   5.752147
4  100004    NaN     25-34   4.837484
sample_df: 
   id  gender age_group     income  happiness
0  0  Female     25-34   1.038463  55.975764
1  1    Male       45+   0.214603  58.645154
2  2    Male     35-44   2.322137  42.285653
3  3     NaN     18-24   0.086068  49.210985
4  4     NaN     35-44  17.156958  49.330845
/home/runner/.local/lib/python3.8/site-packages/balance/datasets/__init__.py:58: UserWarning:

Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

In [3]:
target_df.head().round(2).to_dict()
# sample_df.shape
Out[3]:
{'id': {0: '100000', 1: '100001', 2: '100002', 3: '100003', 4: '100004'},
 'gender': {0: 'Male', 1: 'Male', 2: 'Male', 3: nan, 4: nan},
 'age_group': {0: '45+', 1: '45+', 2: '35-44', 3: '45+', 4: '25-34'},
 'income': {0: 10.18, 1: 6.04, 2: 5.23, 3: 5.75, 4: 4.84}}

In practice, one can use pandas loading function(such as read_csv()) to import data into the DataFrame objects sample_df and target_df.

Load data into a Sample object¶

The first thing to do is to import the Sample class from balance. All of the data we're going to be working with, sample or population, will be stored in objects of the Sample class.

In [4]:
from balance import Sample

Using the Sample class, we can fill it with a "sample" we want to adjust, and also a "target" we want to adjust towards.

We turn the two input pandas DataFrame objects we created (or loaded) into a balance.Sample objects, by using the .from_frame()

In [5]:
sample = Sample.from_frame(sample_df, outcome_columns=["happiness"])
target = Sample.from_frame(target_df)
WARNING (2022-11-23 07:57:00,546) [util/guess_id_column (line 111)]: Guessed id column name id for the data
WARNING (2022-11-23 07:57:00,554) [sample_class/from_frame (line 234)]: No weights passed, setting all weights to 1
WARNING (2022-11-23 07:57:00,565) [util/guess_id_column (line 111)]: Guessed id column name id for the data
WARNING (2022-11-23 07:57:00,579) [sample_class/from_frame (line 234)]: No weights passed, setting all weights to 1

If we use the .df property call, we can see the DataFrame stored in sample. We can see how we have a new weight column that was added (it will all have 1s) in the importing of the DataFrames into a balance.Sample object.

In [6]:
sample.df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000 entries, 0 to 999
Data columns (total 6 columns):
 #   Column     Non-Null Count  Dtype  
---  ------     --------------  -----  
 0   id         1000 non-null   object 
 1   gender     912 non-null    object 
 2   age_group  1000 non-null   object 
 3   income     1000 non-null   float64
 4   happiness  1000 non-null   float64
 5   weight     1000 non-null   int64  
dtypes: float64(2), int64(1), object(3)
memory usage: 47.0+ KB

We can get a quick overview text of each Sample object, but just calling it.

Let's take a look at what this produces:

In [7]:
sample
Out[7]:
(balance.sample_class.Sample)

        balance Sample object
        1000 observations x 3 variables: gender,age_group,income
        id_column: id, weight_column: weight,
        outcome_columns: happiness
        
In [8]:
target
Out[8]:
(balance.sample_class.Sample)

        balance Sample object
        10000 observations x 3 variables: gender,age_group,income
        id_column: id, weight_column: weight,
        outcome_columns: None
        

Next, we combine the sample object with the target object. This is what will allow us to adjust the sample to the target.

In [9]:
sample_with_target = sample.set_target(target)

Looking on sample_with_target now, it has the target atteched:

In [10]:
sample_with_target
Out[10]:
(balance.sample_class.Sample)

        balance Sample object with target set
        1000 observations x 3 variables: gender,age_group,income
        id_column: id, weight_column: weight,
        outcome_columns: happiness
        
            target:
                 
	        balance Sample object
	        10000 observations x 3 variables: gender,age_group,income
	        id_column: id, weight_column: weight,
	        outcome_columns: None
	        
            3 common variables: age_group,gender,income
            

Pre-Adjustment Diagnostics¶

We can use .covars() and then followup with .mean() and .plot() (barplots and qqplots) to get some basic diagnostics on what we got.

We can see how:

  • The proportion of missing values in gender is similar in sample and target.
  • We have younger people in the sample as compared to the target.
  • We have more females than males in the sample, as compared to around 50-50 split for the (non NA) target.
  • Income is more right skewed in the target as compared to the sample.
In [11]:
print(sample_with_target.covars().mean().T)
source                    self     target
_is_na_gender[T.True]  0.08800   0.089800
age_group[T.25-34]     0.30900   0.297400
age_group[T.35-44]     0.17200   0.299200
age_group[T.45+]       0.04600   0.206300
gender[Female]         0.26800   0.455100
gender[Male]           0.64400   0.455100
gender[_NA]            0.08800   0.089800
income                 5.99102  12.737608
In [12]:
print(sample_with_target.covars().asmd().T)
source                  self
age_group[T.25-34]  0.025375
age_group[T.35-44]  0.277771
age_group[T.45+]    0.396127
gender[Female]      0.375699
gender[Male]        0.379314
gender[_NA]         0.006296
income              0.517721
mean(asmd)          0.334860
In [13]:
print(sample_with_target.covars().asmd(aggregate_by_main_covar = True).T)
source          self
age_group   0.233091
gender      0.253769
income      0.517721
mean(asmd)  0.334860
In [14]:
sample_with_target.covars().plot()

Adjusting Sample to Population (ipw and cbps)¶

Next, we adjust the sample to the target. The default method to be used is 'ipw' (which uses inverse probability/propensity weights, after running logistic regression with lasso regularization).

In [15]:
# Using ipw to fit survey weights
adjusted_ipw = sample_with_target.adjust(max_de=None)
INFO (2022-11-23 07:57:01,444) [ipw/ipw (line 408)]: Starting ipw function
INFO (2022-11-23 07:57:01,448) [adjustment/apply_transformations (line 236)]: Adding the variables: []
INFO (2022-11-23 07:57:01,449) [adjustment/apply_transformations (line 237)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2022-11-23 07:57:01,461) [adjustment/apply_transformations (line 274)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2022-11-23 07:57:01,471) [ipw/ipw (line 442)]: Building model matrix
INFO (2022-11-23 07:57:01,578) [ipw/ipw (line 466)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2022-11-23 07:57:01,579) [ipw/ipw (line 469)]: The number of columns in the model matrix: 16
INFO (2022-11-23 07:57:01,581) [ipw/ipw (line 470)]: The number of rows in the model matrix: 11000
INFO (2022-11-23 07:57:01,593) [ipw/ipw (line 501)]: Fitting logistic model
INFO (2022-11-23 07:57:03,468) [ipw/ipw (line 573)]: Chosen lambda for cv: [0.01386604]
INFO (2022-11-23 07:57:03,472) [ipw/ipw (line 581)]: Proportion null deviance explained [0.17374302]
In [16]:
adjusted_cbps = sample_with_target.adjust(method = "cbps")
INFO (2022-11-23 07:57:03,489) [cbps/cbps (line 409)]: Starting cbps function
INFO (2022-11-23 07:57:03,494) [adjustment/apply_transformations (line 236)]: Adding the variables: []
INFO (2022-11-23 07:57:03,496) [adjustment/apply_transformations (line 237)]: Transforming the variables: ['age_group', 'gender', 'income']
INFO (2022-11-23 07:57:03,507) [adjustment/apply_transformations (line 274)]: Final variables in output: ['age_group', 'gender', 'income']
INFO (2022-11-23 07:57:03,623) [cbps/cbps (line 460)]: The formula used to build the model matrix: ['income + gender + age_group + _is_na_gender']
INFO (2022-11-23 07:57:03,627) [cbps/cbps (line 472)]: The number of columns in the model matrix: 16
INFO (2022-11-23 07:57:03,630) [cbps/cbps (line 473)]: The number of rows in the model matrix: 11000
INFO (2022-11-23 07:57:03,645) [cbps/cbps (line 535)]: Finding initial estimator for GMM optimization
INFO (2022-11-23 07:57:03,809) [cbps/cbps (line 562)]: Finding initial estimator for GMM optimization that minimizes the balance loss
WARNING (2022-11-23 07:57:04,214) [cbps/cbps (line 579)]: Convergence of bal_loss function has failed due to 'Maximum number of function evaluations has been exceeded.'
INFO (2022-11-23 07:57:04,216) [cbps/cbps (line 597)]: Running GMM optimization
WARNING (2022-11-23 07:57:04,812) [cbps/cbps (line 612)]: Convergence of gmm_loss function with gmm_init start point has failed due to 'Maximum number of function evaluations has been exceeded.'
WARNING (2022-11-23 07:57:05,409) [cbps/cbps (line 630)]: Convergence of gmm_loss function with beta_balance start point has failed due to 'Maximum number of function evaluations has been exceeded.'
INFO (2022-11-23 07:57:05,416) [cbps/cbps (line 726)]: Done cbps function
In [17]:
print(adjusted_ipw)
        Adjusted balance Sample object with target set using ipw
        1000 observations x 3 variables: gender,age_group,income
        id_column: id, weight_column: weight,
        outcome_columns: happiness
        
            target:
                 
	        balance Sample object
	        10000 observations x 3 variables: gender,age_group,income
	        id_column: id, weight_column: weight,
	        outcome_columns: None
	        
            3 common variables: age_group,gender,income
            
In [18]:
# the adjusted object will look the same as ipw 
print(adjusted_cbps)
        Adjusted balance Sample object with target set using cbps
        1000 observations x 3 variables: gender,age_group,income
        id_column: id, weight_column: weight,
        outcome_columns: happiness
        
            target:
                 
	        balance Sample object
	        10000 observations x 3 variables: gender,age_group,income
	        id_column: id, weight_column: weight,
	        outcome_columns: None
	        
            3 common variables: age_group,gender,income
            

Evaluation of the Results (CBPS vs IPW)¶

We can get a basic summary of the results:

In [19]:
print(adjusted_ipw.summary())
Covar ASMD reduction: 62.3%, design effect: 2.249
Covar ASMD (7 variables): 0.335 -> 0.126
Model performance: Model proportion deviance explained: 0.174
In [20]:
print(adjusted_cbps.summary())
Covar ASMD reduction: 83.8%, design effect: 3.651
Covar ASMD (7 variables): 0.335 -> 0.054

We can see that CBPS did a better job in terms of ASMD reduction. Let's look at it per feature:

We see an improvement in the average ASMD. We can look at detailed list of ASMD values per variables using the following call.

In [21]:
print("ipw:")
print(adjusted_ipw.covars().asmd().T)
print("\ncbps:")
print(adjusted_cbps.covars().asmd().T)
ipw:
source                  self  unadjusted  unadjusted - self
age_group[T.25-34]  0.040094    0.025375          -0.014719
age_group[T.35-44]  0.019792    0.277771           0.257980
age_group[T.45+]    0.137361    0.396127           0.258765
gender[Female]      0.089228    0.375699           0.286472
gender[Male]        0.061820    0.379314           0.317494
gender[_NA]         0.047739    0.006296          -0.041444
income              0.246918    0.517721           0.270802
mean(asmd)          0.126310    0.334860           0.208551

cbps:
source                  self  unadjusted  unadjusted - self
age_group[T.25-34]  0.000231    0.025375           0.025144
age_group[T.35-44]  0.043569    0.277771           0.234202
age_group[T.45+]    0.085689    0.396127           0.310438
gender[Female]      0.012008    0.375699           0.363691
gender[Male]        0.004925    0.379314           0.374388
gender[_NA]         0.029495    0.006296          -0.023200
income              0.104372    0.517721           0.413348
mean(asmd)          0.054337    0.334860           0.280523

It's easier to learn about the biases by just running .covars().plot() on our adjusted object.

In [22]:
adjusted_ipw.covars().plot(library = "seaborn", dist_type = "kde")
In [23]:
adjusted_cbps.covars().plot(library = "seaborn", dist_type = "kde")

We can also use different plots, using the seaborn library, for example with the "kde" dist_type.

Understanding the weights¶

And get the design effect using:

In [24]:
print("ipw:")
print(adjusted_ipw.weights().design_effect())
print("\ncbps:")
print(adjusted_cbps.weights().design_effect())
ipw:
2.24937899455838

cbps:
3.65054410258465

Outcome analysis¶

In [25]:
print(adjusted_ipw.outcomes().summary())
adjusted_ipw.outcomes().plot()
WARNING (2022-11-23 07:57:07,848) [balancedf_class/target_response_rates (line 1307)]: Sample does not have target set
1 outcomes: ['happiness']
Mean outcomes:
            happiness
source               
self        54.221388
unadjusted  48.392784

Response rates (relative to number of respondents in sample):
   happiness
n     1000.0
%      100.0



The estimated mean happiness according to our sample is 48 without any adjustment and 54 with adjustment. The following show the distribution of happinnes:

In [26]:
print(adjusted_cbps.outcomes().summary())
adjusted_cbps.outcomes().plot()
WARNING (2022-11-23 07:57:08,093) [balancedf_class/target_response_rates (line 1307)]: Sample does not have target set
1 outcomes: ['happiness']
Mean outcomes:
            happiness
source               
self        55.825291
unadjusted  48.392784

Response rates (relative to number of respondents in sample):
   happiness
n     1000.0
%      100.0



As we can see, CBPS has a larger design effect, but also fixes more of the ASMD and has an impact on the outcome. So there are pros and cons for each of the two methods.

Downloading data¶

Finally, we can prepare the data to be downloaded for future analyses.

In [27]:
adjusted_cbps.to_download()
Out[27]:
Click here to download: /tmp/tmp_balance_out_8863c0bc-1728-48c9-aafb-1472a48ff57e.csv
In [28]:
# We can prepare the data to be exported as csv - showing the first 500 charaacters for simplicity:
adjusted_cbps.to_csv()[0:500]
Out[28]:
'id,gender,age_group,income,happiness,weight\n0,Female,25-34,1.0384632065106263,55.9757637459091,6.026511810961502\n1,Male,45+,0.21460348629627557,58.645154141877306,0.5642646078847331\n2,Male,35-44,2.3221372597327745,42.28565260361035,6.5445944609493445\n3,,18-24,0.08606802599581903,49.21098472260077,2.5375214138843996\n4,,35-44,17.156958197550072,49.33084508361814,34.43469412961258\n5,,35-44,4.257130738748466,67.46904416515795,15.236678636548142\n6,,25-34,1.0092927734150772,40.03387164850365,6.3842007'
In [28]:
 
In [28]:
 
In [28]: